package com.zhongli.devplatform.controller.sys;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhongli.devplatform.bean.SecurityHelper;
import com.zhongli.devplatform.bean.Res;
import com.zhongli.devplatform.bean.SecurityUserData;
import com.zhongli.devplatform.enums.UserType;
import com.zhongli.devplatform.po.sys.ReadAttentionVideoPO;
import com.zhongli.devplatform.po.sys.ReadSubjectPO;
import com.zhongli.devplatform.po.sys.ReadVideoPO;
import com.zhongli.devplatform.po.user.ReadUserParentPO;
import com.zhongli.devplatform.service.imp.student.ReadUserParentService;
import com.zhongli.devplatform.service.imp.sys.ReadAttentionVideoService;
import com.zhongli.devplatform.service.imp.sys.ReadChapterExercisesService;
import com.zhongli.devplatform.service.imp.sys.ReadSubjectService;
import com.zhongli.devplatform.service.imp.sys.ReadVideoService;
import com.zhongli.devplatform.utils.user.UserUtil;
import com.zhongli.devplatform.vo.sys.SubjectVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wy
 * @since 2020/7/31
 */
@Api(tags = "科目", description = "科目，知识库首页")
@RestController
@RequestMapping("/subject")
public class SubjectController {

    @Autowired
    private ReadSubjectService subjectService;

    @Autowired
    private ReadAttentionVideoService attentionVideoService;

    @Autowired
    private ReadVideoService videoService;

    @Autowired
    private ReadChapterExercisesService chapterExercisesService;


    @Autowired
    private ReadUserParentService userParentService;

    @ApiOperation("根据当前学生 查询所在班级的 所有科目")
    @GetMapping("/allCurrentStuSubject")
    public Res<List<ReadSubjectPO>> getCurrentSubject(){
        String userRole = UserUtil.getUserRole();
        Integer currentStu = SecurityHelper.getCurrentUserId();
        if("parent".equals(userRole)){
            ReadUserParentPO userParentPO = userParentService.getOne(new QueryWrapper<ReadUserParentPO>()
                    .eq("parent_id",currentStu));
            currentStu = userParentPO.getUserId();
        }
        List<ReadSubjectPO> data = subjectService.getStudentSubject(currentStu);
        return Res.ok(data);
    }

    @ApiOperation("根据当前学生 查询所在班级的 所有科目")
    @GetMapping("/getStudentSubject/{id}")
    public Res<List<ReadSubjectPO>> getStudentSubject(@PathVariable("id") Integer id){
        List<ReadSubjectPO> data = subjectService.getStudentSubject(id);
        return Res.ok(data);
    }

    @ApiOperation("查询全部科目")
    @GetMapping
    public Res<List<ReadSubjectPO>> findAll(){
        List<ReadSubjectPO> list = subjectService.list(new QueryWrapper<ReadSubjectPO>()
                .eq("is_delete",0));
        return Res.ok(list);
    }

    @ApiOperation("根据当前学生 查询所在关注视频的 所有科目")
    @GetMapping("/getCurrentStuAttVideoSubject")
    public Res<List<ReadSubjectPO>> getCurrentAttVideo(){
        Integer currentStu = SecurityHelper.getCurrentUserId();
        List<ReadAttentionVideoPO> tempVideoIds = attentionVideoService.list(new QueryWrapper<ReadAttentionVideoPO>().eq("user_id", currentStu));
        if (tempVideoIds == null) return Res.ok(new ArrayList<>());
        List<Integer> videoIds = tempVideoIds
                .stream()
                .map(ReadAttentionVideoPO::getVideoId)
                .collect(Collectors.toList());
        List<ReadVideoPO> videos = videoService.listByIds(videoIds);
        List<Integer> subjectIds = videos.stream()
                .map(ReadVideoPO::getSubjectId)
                .distinct()
                .collect(Collectors.toList());
        List<ReadSubjectPO> data = subjectService.list(new QueryWrapper<ReadSubjectPO>()
                .in("id",subjectIds).eq("is_delete",0));
        return Res.ok(data);
    }


    @ApiOperation("根据当前学生 查询所有的**错题** 科目")
    @GetMapping("/getAllWrongSubjects")
    public Res<List<SubjectVO>> getAllWrongSubjects(){
        Integer currentStu = SecurityHelper.getCurrentUserId();
        List<SubjectVO> data = chapterExercisesService.getAllWrongSubjects(currentStu);
        return Res.ok(data);
    }

}
